<?php
namespace Admin\Controller;
use Admin\Model\DatadictModel;


class ProjectGLController extends AbstractController  {
	
	protected $From;

	public function _initialize(){
		$this->assign("busid",I("busid"));
		$this->assign("formid",I("formid"));
		$this->From = D('Project');
    }

    public function index(){
    	$id = (int)I('id');
    	$this->assign("projectID",$id);
    	$res = $this->From->MyshowList();
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
        $this->display("Project/ProjectGL/index");
    }
	
	public function save($type = false, $id = 0){
		if($type != false){
			if($type == "add"){
				if(!$this->From->create()){
					exit($this->error($this->From->getError()));
				}else{
					$result = $this->From->add();
					if($result){
						$this->success('添加成功！');
					}else{
						$this->error('添加错误！');
					}
				}
			}else if($type == "edit"){
				if(I('id')){
					$res = $this->From->Myshow(array("ID" => (int)I('id')));
					$this->assign("editdata",$res[0]);
					$this->assign("data",$this->getsaveViewInitData());
				}
				$this->display("Project/ProjectGL/save");
			}else if($type == "update"){
				$in = I();
				$this->From->compareProject($in);
				$result = $this->From->Myupdata();
				if($result['state'] == 1) $this->From->Snapshot();
			}else if($type == "delete"){
				$result = $this->From->Mydelet();
			}
			if($result['state'] == 1 && !empty($result)){
				$this->success($result['msg']);
			}else if($result['state'] == 0 && !empty($result)){
				$this->error($result['msg']);
			}
		}else{
			$this->assign("data",$this->getsaveViewInitData());
			$this->display("Project/ProjectGL/save");
		}
	}

	/**
	 * 
	 * 获取编辑页面初始数据
	 */
	public function getsaveViewInitData(){
		$return = array();
		$Datadict = new DatadictModel();
		$FundSourceID = $Datadict->Myshow(array("Name" => '资金来源', "ParentID" => 0));
		$return['selectlist']['FundSource'] = $FundSourceRes = $Datadict->Myshow(array("ParentID" => $FundSourceID[0]['ID']));
		if(!empty($FundSourceID)){
			$return['selectlist']['Scale'] = $ScaleRes = $Datadict->Myshow(array("ParentID" => $FundSourceID[0]['ID']));
		}
		$ScaleID = $Datadict->Myshow(array("Name" => '项目规模', "ParentID" => 0));
		if(!empty($ScaleID)){
			$return['selectlist']['Scale'] = $ScaleRes = $Datadict->Myshow(array("ParentID" => $ScaleID[0]['ID']));
		}
		$ProjectTypeID = $Datadict->Myshow(array("Name" => '项目类型', "ParentID" => 0));
		if(!empty($ProjectTypeID)){
			$return['selectlist']['ProjectType'] = $ProjectTypeRes = $Datadict->Myshow(array("ParentID" => $ProjectTypeID[0]['ID']));
		}
		$Status = $Datadict->Myshow(array("Name" => '项目进行状态', "ParentID" => 0));
		if(!empty($Status)){
			$return['selectlist']['Status'] = $ProjectTypeRes = $Datadict->Myshow(array("ParentID" => $Status[0]['ID']));
		}
		$return['selectlist']['TID'] = $this->From->getTaskList();	
		//$return['selectlist'] = array_merge($FundSourceRes,$ScaleRes,$ProjectTypeRes);
		return $return;
	}
	

	
}